﻿//该类为花图案绘制
//vesion 1.0
//author 夏天的树人
package org.summerTree.graphics
{
	import flash.geom.Point;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.IBitmapDrawable;
	public class Flower extends Base
	{

		//private var LineColor:uint=0x000000;//默认是黑色
		private var faceManager:Sprite=new Sprite();
		public function Flower()
		{
             addChild(faceManager);
		}
		override public function clone():*
		{
			return new Flower();
		}
		
		//添加额外的图形到花朵上
		public function addFace(SourceData:IBitmapDrawable,Width:Number,Height:Number):void
		{
			var bitmapdata:BitmapData=new BitmapData(Width,Height,false,0xffffff);
            bitmapdata.lock();
			bitmapdata.draw(SourceData);	
			bitmapdata.unlock();
			
			var face:Bitmap=new Bitmap(bitmapdata);
			face.x=-bitmapdata.width/2;
			face.y=-bitmapdata.height/2;
			
			var shape:Shape=new Shape();
			faceManager.addChild(shape);
			shape.graphics.lineStyle(1);
			shape.graphics.beginFill(0xff0000);
			shape.graphics.drawCircle(0,0,Width/2);			
			shape.graphics.endFill();
			
			face.mask=shape;
			faceManager.addChild(face);		
		}		
		
		//删除脸谱
		public function removeFace():void
		{
			while(faceManager.numChildren>0)
			faceManager.removeChildAt(0);								
		}
			
		/*radius 半径
		 *n 数目
		 *fillColor 填充颜色
		 *alpha  透明度
		 */		
		public function CreatFlower(radius:Number,n:Number=10,fillColor:Number=0xFFFF00,alpha:Number=1):void
		{
            if(n<=0) return;
			this.graphics.lineStyle(1,color);
			this.graphics.drawCircle(0,0,radius);
			var angle:Number=2*Math.PI/n;
			var Length:Number=getLength(radius,angle);
			trace(Length)
			for(var i:int=0;i<n;i++)
			{			
			  var point:Point=Point.polar(radius,i*angle);
			  this.graphics.beginFill(fillColor,alpha);
			  this.graphics.drawCircle(point.x,point.y,Length);		
			  this.graphics.endFill();
			}
			this.graphics.beginFill(0xffffff,1);
			this.graphics.drawCircle(0,0,radius);
			this.graphics.endFill();

		}
		//计算出两个圆的连线长度,即计算出单个圆的半径
		private function getLength(radius:Number,angle:Number):Number
		{
			var Length:Number;
			Length=2*radius*radius-2*radius*radius*Math.cos(angle);
			return Math.sqrt(Length)/2;			
		}
		
		//
//		//设置颜色
//		public function set color(value:uint):void
//		{
//			this.LineColor=value;
//		}
//		//获取颜色
//		public function get color():uint
//		{
//			return this.LineColor;
//		}
		////清除图像
//		public function clear():void
//		{
//			this.graphics.clear();
//		}
	}
}